package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import benny_driving_ser.dao.DriStateDao;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.FormatUtil;

public class DriStateDaoImpl implements DriStateDao{

	public int queryStateById(String dri_id) {
		
		int state =3;
		String sql_query = "select id,state,stop_time from t_user where id=?";
		Connection conn = DBUtil.getInstance().openConnection();
		try{
			PreparedStatement pst = conn.prepareStatement(sql_query);
			pst.setInt(1, Integer.parseInt(dri_id));
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				
				if(rs.getString("stop_time")==null||(System.currentTimeMillis())>(FormatUtil.dateString2long(rs.getString("stop_time"))+(86400000*3))){
					state = rs.getInt("state");
				}else{
					state = 2;
				}
			}
			rs.close();
			pst.close();
			
		}catch(SQLException e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			DBUtil.getInstance().closeConnection(conn);
		}
		
		return state;
	}

}
